﻿using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Web.UI.WebControls;
using Business;
using Entities;
namespace ServicesDirectory.UserControl
{
    public partial class ListProgramme : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAll();
            }
            All.Click += All_Click;
            lbt09.Click += lbt09_Click;
            lbtA.Click += lbtA_Click;
            lbtF.Click += lbtF_Click;
            lbtK.Click += lbtK_Click;
            lbtO.Click += lbtO_Click;
            lbtS.Click += lbtS_Click;
            lbtW.Click += lbtW_Click;
            Inactive.CheckedChanged += chkInActive_CheckedChanged;
            lbtProgrammeName.Click += lbtProgrammeName_Click;

        }

        /// <summary>
        /// Handles the Click event of the lbtProgrammeName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtProgrammeName_Click(object sender, EventArgs e)
        {
            if (ViewState["sort"] == null || ViewState["sort"].ToString().Equals("asc"))
            {
                Collection<Programme> result =
                    new Collection<Programme>(ProgrammeBusiness.Instance.Filter(
                        ViewState["filter"].ToString(), Inactive.Checked).
                                       OrderBy(item => item.ProgrammeName).ToList());
                Paging(result);
                ViewState["sort"] = "desc";
            }
            else
            {
                Collection<Programme> result =
                    new Collection<Programme>(ProgrammeBusiness.Instance.Filter(
                        ViewState["filter"].ToString(), Inactive.Checked).
                                       OrderByDescending(item => item.ProgrammeName).ToList());
                Paging(result);
                ViewState["sort"] = "asc";
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the chkInActive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void chkInActive_CheckedChanged(object sender, EventArgs e)
        {
            if (ViewState["filter"] == null)
            {
                var result = ProgrammeBusiness.Instance.ShowListProgramme(Inactive.Checked);
                if (result != null)
                {
                    Paging(result);
                }
            }
            else
            {
                var result = ProgrammeBusiness.Instance.Filter(ViewState["filter"].ToString(), Inactive.Checked);
                if (result != null)
                {
                    Paging(result);
                }
            }
        }
        /// <summary>
        /// Loads all.
        /// </summary>
        public void LoadAll()
        {

            ViewState["filter"] = "all";
            var result = ProgrammeBusiness.Instance.ShowListProgramme(Inactive.Checked);

            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the Create control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Create_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddProgramme.aspx");
        }

        #region filter funtion - do filfer base on keyword


        /// <summary>
        /// Handles the Click event of the lbtW control- filter by W X Y Z keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtW_Click(object sender, EventArgs e)
        {
            ViewState["filter"] = "wz";
            var result = ProgrammeBusiness.Instance.Filter("wz", Inactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtS control - filter by S T U V keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtS_Click(object sender, EventArgs e)
        {

            ViewState["filter"] = "sv";
            var result = ProgrammeBusiness.Instance.Filter("sv", Inactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtO control - filter by O P Q R keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtO_Click(object sender, EventArgs e)
        {

            ViewState["filter"] = "or";
            var result = ProgrammeBusiness.Instance.Filter("or", Inactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtK control - filter by K L M N keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtK_Click(object sender, EventArgs e)
        {

            ViewState["filter"] = "kn";
            var result = ProgrammeBusiness.Instance.Filter("kn", Inactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtF control - filter by F G H I J keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtF_Click(object sender, EventArgs e)
        {

            ViewState["filter"] = "fj";
            var result = ProgrammeBusiness.Instance.Filter("fj", Inactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbtA control filter by  A B C D E keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbtA_Click(object sender, EventArgs e)
        {

            ViewState["filter"] = "ae";
            var result = ProgrammeBusiness.Instance.Filter("ae", Inactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click event of the lbt09 control filter by 0-9 keywords
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void lbt09_Click(object sender, EventArgs e)
        {

            ViewState["filter"] = "09";
            var result = ProgrammeBusiness.Instance.Filter("09", Inactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }


        /// <summary>
        /// Handles the Click event of the All control- filter ALL
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void All_Click(object sender, EventArgs e)
        {

            ViewState["filter"] = "all";
            var result = ProgrammeBusiness.Instance.Filter("all", Inactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        #endregion


        /// <summary>
        /// Pagings the specified list programme.
        /// </summary>
        /// <param name="listProgramme">The list programme.</param>
        private void Paging(Collection<Programme> listProgramme)
        {
            lblTotalPage.Text = (listProgramme.Count % 15 == 0 ? listProgramme.Count / 15 : listProgramme.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
            //paging on list result
            var pagedList = ProgrammeBusiness.Instance.Paging(int.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture), 15, listProgramme);
            //bind paged data
            Repeater1.DataSource = pagedList;
            Repeater1.DataBind();
        }

        #region paging buttons

        /// <summary>
        /// Handles the Click1 event of the Fisrt control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Fisrt_Click1(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            var result = ProgrammeBusiness.Instance.Filter(ViewState["filter"].ToString(), Inactive.Checked);

            if (result != null)
            {
                Paging(result);
            }
        }

        /// <summary>
        /// Handles the Click1 event of the Back control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Back_Click1(object sender, EventArgs e)
        {
            if (int.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) > 1)
            {
                txtPageNumber.Text = (int.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) - 1).ToString(CultureInfo.InvariantCulture);
                var result =
                    ProgrammeBusiness.Instance.Filter(
                        ViewState["filter"].ToString(), Inactive.Checked);
                if (result != null)
                {
                    Paging(result);
                }
            }
        }

        /// <summary>
        /// Handles the Click1 event of the Next control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Next_Click1(object sender, EventArgs e)
        {
            if (int.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) < int.Parse(lblTotalPage.Text, CultureInfo.CurrentCulture))
            {
                txtPageNumber.Text = (int.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) + 1).ToString(CultureInfo.InvariantCulture);
                var result =
                    ProgrammeBusiness.Instance.Filter(
                        ViewState["filter"].ToString(), Inactive.Checked);
                if (result != null)
                {

                    Paging(result);
                }
            }
        }

        /// <summary>
        /// Handles the Click1 event of the Last control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Last_Click1(object sender, EventArgs e)
        {
            txtPageNumber.Text = lblTotalPage.Text;
            var result =
                        ProgrammeBusiness.Instance.Filter(
                            ViewState["filter"].ToString(), Inactive.Checked);
            if (result != null)
            {
                Paging(result);
            }
        }

        #endregion

        /// <summary>
        /// Handles the ItemDataBound event of the Repeater1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterItemEventArgs" /> instance containing the event data.</param>
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lblIsActive = e.Item.FindControl("lblActive") as Label;
            if (lblIsActive != null)
                lblIsActive.Text = lblIsActive.Text == "True" ? "Yes" : "No";
        }

        /// <summary>
        /// Handles the ItemCommand event of the Repeater1 control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterCommandEventArgs" /> instance containing the event data.</param>
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

            if (e.CommandName == "EditProgramme")
            {
                var lblActive = e.Item.FindControl("lblActive") as Label;
                if (lblActive != null)
                {
                    var curentProgramme = ProgrammeBusiness.Instance.GetProgrammeById(e.CommandArgument.ToString());
                    if (ProgrammeBusiness.Instance.CheckLinktoService(curentProgramme.ContactId.ToString()))
                        if (lblActive.Text == "No")
                        {
                            Response.Write("<script type=\"text/javascript\">");
                            Response.Write("var result= window.confirm('This Programme is already in use, do you want to make this in-active?'); ");
                            Response.Write("if(result)");
                            Response.Write(" window.open('EditProgramme.aspx?id=" + e.CommandArgument + "');");
                            Response.Write("</script>");
                        }
                        else
                        {
                            Response.Redirect("EditProgramme.aspx?id=" + e.CommandArgument);
                        }
                    else
                    {
                        if (lblActive.Text == "No")
                        {
                            Response.Write("<script type=\"text/javascript\">");
                            Response.Write("var result= window.confirm('Do you want to mark this Service active'); ");
                            Response.Write("if(result)");
                            Response.Write(" window.open('EditProgramme.aspx?id=" + e.CommandArgument + "');");
                            Response.Write("</script>");
                        }
                        else
                        {
                            Response.Redirect("EditProgramme.aspx?id=" + e.CommandArgument);
                        }
                    }


                }

            }
        }


    }
}